package com.dangs.autotest.system;

import android.content.Context;
import android.util.Log;

import com.dangs.autotest.tool.L;
import com.mstar.android.tv.TvS3DManager;
import com.mstar.android.tvapi.common.TvManager;
import com.mstar.android.tvapi.common.exception.TvCommonException;

import java.io.IOException;
import java.io.RandomAccessFile;

public class F3InterfaceImp extends CommonInterfaceImp {

    @Override
    public String getCpuLoadRate() {
        RandomAccessFile reader = null;
        try {
            reader = new RandomAccessFile("/proc/loadavg", "r");
            String load = reader.readLine();
            String[] tmp = load.split(" ");
            String rate = tmp[1] + "%";
            return rate;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return "none";
    }

    @Override
    public void reboot(Context context) {
        setEnv("factory_poweron_mode", "direct");
        execCMD("reboot");
    }

    @Override
    public TYPE_3D get3DType() {
        int type = TvS3DManager.getInstance().get3DTo2DDisplayMode();
        switch (type) {
            case TvS3DManager.THREE_DIMENSIONS_DISPLAY_FORMAT_NONE:
                return TYPE_3D.TYPE_CLOSE;
            case TvS3DManager.THREE_DIMENSIONS_DISPLAY_FORMAT_2DTO3D:
                return TYPE_3D.TYPE_2D_TO_3D;
            case TvS3DManager.THREE_DIMENSIONS_DISPLAY_FORMAT_SIDE_BY_SIDE:
                return TYPE_3D.TYPE_LEFT_RIGHT;
            case TvS3DManager.THREE_DIMENSIONS_DISPLAY_FORMAT_TOP_BOTTOM:
                return TYPE_3D.TYPE_UP_DOWN;
        }
        return TYPE_3D.TYPE_CLOSE;
    }

    @Override
    public boolean set3DType(TYPE_3D type) {
        boolean ret = false;
        TvS3DManager manager = TvS3DManager.getInstance();
        if (manager == null) {
            Log.w(TAG, "");
        }
        switch (type) {
            case TYPE_CLOSE:
                ret = manager.set3dDisplayFormat(TvS3DManager.THREE_DIMENSIONS_DISPLAY_FORMAT_NONE);
                break;
            case TYPE_UP_DOWN:
                ret = manager.set3dDisplayFormat(TvS3DManager.THREE_DIMENSIONS_DISPLAY_FORMAT_SIDE_BY_SIDE);
                break;
            case TYPE_LEFT_RIGHT:
                ret = manager.set3dDisplayFormat(TvS3DManager.THREE_DIMENSIONS_DISPLAY_FORMAT_TOP_BOTTOM);
                break;
            case TYPE_2D_TO_3D:
                ret = manager.set3dDisplayFormat(TvS3DManager.THREE_DIMENSIONS_DISPLAY_FORMAT_2DTO3D);
                break;
            default:
                Log.w(TAG, "no 3d type " + type);

        }
        return ret;
    }

    @Override
    public String getEnv(String key, String defaultValue) {
        String value = defaultValue;
        try {
            TvManager.getInstance().getEnvironment(key);
        } catch (TvCommonException e) {
            L.w(TAG, "failed to get env:" + key);
        }
        return value;
    }

    @Override
    public void setEnv(String key, String value) {
        try {
            TvManager.getInstance().setEnvironment(key, value);
        } catch (TvCommonException e) {
            L.w(TAG, "failed to get env:" + key);
        }
    }

    @Override
    public void focusByManual(boolean zoomIn) {
        if (zoomIn) {
            write("sys/class/projector/LensZoom", "wt 1 1e 1");
        } else {
            write("sys/class/projector/LensZoom", "wt 0 1e 1");
        }
    }

    @Override
    public void focusByCamera() {
//        SystemProperties.set("persist.sys.aiclass.cam_fcs", "on");
        SystemProperties.set("init.sys.aiclass.mode", "2");
    }

    @Override
    public int readFocusStep() {
        int step = 0;
        try {
            step = Integer.parseInt(readLine("sys/class/projector/LensZoom"));
        } catch (NumberFormatException e) {
            L.w(TAG, "read step fail:" + e);
        }
        return step;
    }

    @Override
    public void focusByTof() {
        SystemProperties.set("init.sys.aiclass.mode", "1");
    }

    @Override
    public void trapezoid() {
        SystemProperties.set("persist.sys.aiclass.auto_avoid", "off");
        SystemProperties.set("persist.sys.aiclass.auto_cfill", "off");
        SystemProperties.set("init.sys.aiclass.mode", "29");
    }

    @Override
    public void trapezoidWithFill() {
        SystemProperties.set("persist.sys.aiclass.auto_avoid", "off");
        SystemProperties.set("persist.sys.aiclass.auto_cfill", "on");
        SystemProperties.set("init.sys.aiclass.mode", "29");
    }

    @Override
    public void trapezoidWithAvoid() {
        SystemProperties.set("persist.sys.aiclass.auto_avoid", "on");
        SystemProperties.set("persist.sys.aiclass.auto_cfill", "off");
        SystemProperties.set("init.sys.aiclass.mode", "29");
    }

    @Override
    public void trapezoidWith3In1() {
        SystemProperties.set("persist.sys.aiclass.auto_avoid", "on");
        SystemProperties.set("persist.sys.aiclass.auto_cfill", "on");
        SystemProperties.set("init.sys.aiclass.mode", "29");
    }

}
